﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
namespace Com
{
	/// <summary>
	/// Interaction logic for ucAirRoof.xaml
	/// </summary>
	public partial class ucAirRoof : UserControl
	{
		public ucAirRoof()
		{
			this.InitializeComponent();
            loadData();
            enableButtons();
		}

        private void loadData()
        {
            cbxSurface.Items.Clear();
            LibraryManagerInCom.loadSurfaceCoefficient();
            foreach (var item in LibraryManagerInCom.RoofSurfaceList)
            {
                cbxSurface.Items.Add(item);
            }
            cbxSurface.DisplayMemberPath = "surface_roof";
            cbxSurface.SelectedIndex = 0;
        }
        private void enableButtons()
        {
            btnCancel.IsEnabled = false;
            btnSave.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;
        }
        private void disableButtons()
        {
            btnCancel.IsEnabled = true;
            btnSave.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }

        private void cbxSurface_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxSurface.SelectedItem != null)
            {
                txtRadiation.Text = ((tb_roof_surface)cbxSurface.SelectedItem).absorption_coefficient.ToString();
            }
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxSurface.Text = "";
            txtRadiation.Text = "";
            disableButtons();

        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_roof_surface rs = LibraryManagerInCom.copyRoofSurface((tb_roof_surface)cbxSurface.SelectedItem);
            cbxSurface.Text = rs.surface_roof;
            txtRadiation.Text = rs.absorption_coefficient.ToString();
            disableButtons();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                try
                {
                    tb_roof_surface rs = (tb_roof_surface)cbxSurface.SelectedItem;
                    LibraryManagerInCom.delRoofSurface(rs);
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
           
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_roof_surface rs = new tb_roof_surface();
                rs.surface_roof = cbxSurface.Text;
                rs.absorption_coefficient = Convert.ToDouble(txtRadiation.Text);
                LibraryManagerInCom.addRoofSurface(rs);
                loadData();
                enableButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        
        }
	}
}